<?php

namespace App\Http\Controllers\Web;

use App\Handlers\ArticleAnchorsHandler;
use App\Http\Controllers\Controller;
use App\Models\Anchor;
use App\Repositories\NewRepository;
use Illuminate\Http\Request;

class NewsController extends Controller
{
    private $newRepository;



    public function __construct(NewRepository $newRepository)
    {

        $this->newRepository = $newRepository;
    }

    public function index(Request $request){


        $news = $this->newRepository->paginate(6);

        return view('web.news.index')->with('news',$news);
    }


    public function show($id){

        $news = $this->newRepository->find(intval($id));
        if(!$news){
            abort(404);
        }

        $next = $this->newRepository->getNextArticle($id,$news->article_cate_id);
        $prev = $this->newRepository->getPrevArticle($id,$news->article_cate_id);
        $news->content = app(ArticleAnchorsHandler::class)->setAnchors($news->content,Anchor::get()->toArray());
        //dd(Anchor::get()->toArray());
        $news->content = app(ArticleAnchorsHandler::class)->relatedArticle($news->content,$id);

        return view('web.news.show',compact('news','next','prev'));

    }
}
